package com.xs.ctl;

import org.jivesoftware.smack.packet.PacketExtension;

public class ExtensionCmdXml implements PacketExtension{

	public static final String ELEMENT = "lomo";
	public static final String NAMESPACE = "lomo:cmd";
	
	private String StrCmd = null;
	private String StrCmdValue = null;
	private comAttr stComAttr = new comAttr();
	
	public String getCmd(){
		return StrCmd;
	}
	public void setCmd(String cmd){
		this.StrCmd = cmd;
	}
	
	public String getCmdValue(){
		return StrCmdValue;
	}
	public void setCmdValue(String value){
		this.StrCmdValue = value;
	}
	
	public void setComAttr(int iNo, String strBaudrate, String strDataBit, int iStopBit, int iParity, int iFlowCtl){
		stComAttr.iComNo = iNo;
		stComAttr.strBaudrate = strBaudrate;
		stComAttr.strDatabit = strDataBit;
		stComAttr.iStopBit = iStopBit;
		stComAttr.iParity = iParity;
		stComAttr.iFlowCtl = iFlowCtl;
	}
	
	public comAttr getComAttr(){
		return stComAttr;
	}
	@Override
	public String getElementName() {
		// TODO Auto-generated method stub
		return ELEMENT;
	}

	@Override
	public String getNamespace() {
		// TODO Auto-generated method stub
		return NAMESPACE;
	}

	@Override
	public String toXML() {
		String sb = null;
//		<presence id="lomo_00000058" type="result" to="xssl2000@gmail.com/lomo9D9AC423" from="mqbest2000@gmail.com/lomoDDCE8F76">
//        <config>
//          <com baudrate="115200" stopbit="1" parity="0" flowctrl="0" no="1" databit="8"/>
//          <resource res="lomo_wh"/>
//          <destsn sn="mqbest2000"/>
//        </config>
//		</presence>	
		if("1".equals(StrCmd)){ // set config
			sb = "<" + ELEMENT + " xmlns=\"" + NAMESPACE + "\" "+ "cmd=\"" +StrCmd + "\"" + ">"+
		                       "<com "+	"no="      +"\""+ stComAttr.iComNo + "\" "+
		                       			"baudrate="+"\""+ stComAttr.strBaudrate + "\" "+
		                       			"databit=" +"\""+ stComAttr.strDatabit  + "\" "+
										"stopbit=" +"\""+ stComAttr.iStopBit  + "\" "+
										"parity="  +"\""+ stComAttr.iParity   + "\" "+
										"flowctrl="+"\""+ stComAttr.iFlowCtl  + "\""+"/>"+
								"<master "+ "mSn=" + "\"" + "mqbest2000"+ "\""+ "/>"+
								"<dest "  + "dSn="  + "\"" + "xssl2001"  + "\""+ "/>"+
				"</" + ELEMENT + ">";
		}else{
			if(null != StrCmdValue){
				sb = "<" + ELEMENT + " xmlns=\"" + NAMESPACE + "\" "+ "cmd=\"" +StrCmd + "\" " + "para=\"" +StrCmdValue + "\"" + "/>";
			}else{
				sb = "<" + ELEMENT + " xmlns=\"" + NAMESPACE + "\" "+ "cmd=\"" +StrCmd + "\"" + "/>";
			}		
		}
		return sb;
	}
	
	public static String createContinueString(String... strings) {
	    StringBuffer sb = new StringBuffer();
	    for (String string : strings) {
	        sb.append(string);
	    }
	    return sb.toString();
	}
}


